package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.service.PasswordService;
import cn.com.yusys.yusp.uaa.client.dto.CodeAndMessage;
import cn.com.yusys.yusp.uaa.client.dto.PasswordCheckDto;
import cn.com.yusys.yusp.uaa.web.rest.PasswordStrategyResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api/password"})
public class PasswordResource {

    private static final String NO_STRATEGY = "1002";
    private static final String UNSUCCESSFULLY_CHECKED = "1003";
    private static final String SUCCESSFULLY_CHECKED = "1001";
    private static final String UNSUCCESSFULLY_DECRYPTED = "1004";
    @Autowired
    private PasswordService service;
    private Logger logger = LoggerFactory.getLogger(PasswordStrategyResource.class);


    public static String getNoStrategy() {
        return "1002";
    }

    public static String getUnsuccessfullyChecked() {
        return "1003";
    }

    public static String getSuccessfullyChecked() {
        return "1001";
    }

    public static String getUnsuccessfullyDecrypted() {
        return "1004";
    }

    @PostMapping({"/checkpwd"})
    public ResponseEntity<CodeAndMessage> checkPwd(@RequestBody PasswordCheckDto passwordCheckDto) {
        String pwd = passwordCheckDto.getPwd();
        String sysId = passwordCheckDto.getSysId();
        String passwordType = passwordCheckDto.getPasswordType();
        String userId = passwordCheckDto.getUserId();
        String info = "";
        if ("2".equals(passwordType)) {
            try {
                info = this.service.decrypt(pwd);
            } catch (Exception var8) {
                return new ResponseEntity(new CodeAndMessage(String.valueOf(10100004), "密文编码错误，请检查密码"), HttpStatus.BAD_REQUEST);
            }
        } else {
            info = pwd;
        }

        if (null != info && !"".equals(info)) {
            CodeAndMessage result = this.service.checkPwd(sysId, info, userId);
            return new ResponseEntity(result, HttpStatus.OK);
        } else {
            return new ResponseEntity(new CodeAndMessage(String.valueOf(10100005), "密码解析失败，请检查密码"), HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping({"/match"})
    public ResponseEntity<CodeAndMessage> equal2UsingPwd(@RequestBody PasswordCheckDto passwordCheckDto) {
        this.logger.info("Match login user password");
        String pwd = passwordCheckDto.getPwd();
        String pwdType = passwordCheckDto.getPasswordType();
        String userId = passwordCheckDto.getUserId();
        CodeAndMessage result = this.service.matchUsingPwd(pwd, pwdType, userId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

}
